//
//  SrolledPictureCell.swift
//  PictureBrowser
//
//  Created by kingseraphs on 27/04/2017.
//  Copyright © 2017 kingseraphs. All rights reserved.
//

import UIKit

class SrolledPictureCell: UITableViewCell,UITableViewDelegate,UITableViewDataSource{

    @IBOutlet weak var scrolledContentView: UIView!
    private lazy var scrolledPictureTb = UITableView()
    
    override func awakeFromNib() {
        super.awakeFromNib()

        scrolledContentView.addSubview(scrolledPictureTb)
        scrolledPictureTb.delegate = self
        scrolledPictureTb.dataSource = self
        scrolledPictureTb.register(UINib(nibName: "PictureViewCell", bundle: nil), forCellReuseIdentifier: "cellId")
        scrolledPictureTb.register(UITableViewCell.self, forCellReuseIdentifier: "sss")
    }
    
    override func layoutSubviews() {
        contentView.layoutIfNeeded()
        
        scrolledPictureTb.transform = CGAffineTransform(rotationAngle: CGFloat(-Double.pi/2))
        scrolledPictureTb.frame = CGRect(x: 0, y: 0, width: Int(UIScreen.main.bounds.width), height: Int(scrolledContentView.bounds.height))
        scrolledContentView.setNeedsLayout()
    }
}

extension SrolledPictureCell {
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return scrolledContentView.bounds.height
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 8
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cellId", for: indexPath)
        cell.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi / 2))
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: false)
        
        let pBrowerVc = PictureBrowerViewController()
        //系统自带的4种
//        pBrowerVc.modalTransitionStyle = .flipHorizontal
        //CATransition
        let transition = CATransition()
        transition.duration = 1.8
        transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
        transition.type = "rippleEffect"
        transition.subtype = kCATransitionFromLeft
        UIApplication.shared.keyWindow?.rootViewController?.view.window?.layer.add(transition, forKey: nil)
        UIApplication.shared.keyWindow?.rootViewController?.present(pBrowerVc, animated: true, completion: nil)
    }
}
